﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp001
{
    public partial class MainFrom : Form
    {
        public MainFrom()
        {
            InitializeComponent();
        }

        private void MainFrom_Load(object sender, EventArgs e)
        {
            var sqlArt = "select * from Articles";

            var dt = DbHelper.GetDataTable(sqlArt);

              dataGridView1.DataSource = dt;

            //界面
            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.AllowUserToAddRows = false;

        }
        //查找
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Author like '%{0}%'", name);

            var dt = DbHelper.GetDataTable(sql);
            
            dataGridView1.DataSource = dt;

        }

        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            Editfrom editfrom = new Editfrom();
            editfrom.Show();
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int )dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var author = (string )dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var title = (int )dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var createdtim = (string )dataGridView1.SelectedRows[0].Cells["CreatedTim"].Value;

                Editfrom editfrom = new Editfrom(id,author,title,createdtim);
                editfrom.Show();
            }
            else
            {
                MessageBox.Show("你未选择数据行", "提示");
            }
           
        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;


                var res = MessageBox.Show("确定删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (rowCount > 0)
                    {
                        var name = textBox1.Text;
                        var sqlArt =string .Format( "select * from Articles where Authof like '% {0}%'",name);

                        var dt = DbHelper.GetDataTable(sqlArt);

                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功", "提示");
                    }

                }
                else
                {
                    MessageBox.Show("没有选择任何的行", "提示");
                }

            }
          
            

        }
    }
}
